/**
 * ===========================================================================
 * 版权所有 湖南免了么网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.mianle.me
 * ===========================================================================
 * 15:41 2018/6/26
 */
package com.mianle.userapi.service.task05;

import com.mianle.userapi.mapper.GaodeMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.TriggerContext;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.concurrent.Callable;
import java.util.concurrent.ScheduledFuture;

/**
 * @Author: yiyuan
 * @Description:
 * @Date: Created in 15:41 2018/6/26
 *
 */
@Component
@Slf4j
public class TaskConfig {

    @Autowired
    private ThreadPoolTaskScheduler threadPoolTaskScheduler;

    @Autowired
    private GaoServer gaoServer;

    private ScheduledFuture<?> task;

    @Bean
    public ThreadPoolTaskScheduler threadPoolTaskScheduler() {
        return new ThreadPoolTaskScheduler();
    }

    public void start(final String cron) {
        //取消任务
        stopTask();
        task = threadPoolTaskScheduler.schedule(new TaskRun(gaoServer),  new CronTrigger(cron));
    }

    public void stopTask() {
        if (task != null) {
            //取消任务调度
            task.cancel(true);
        }
    }
}
